package com.hiultra.archive.utils;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hiultra.archive.domain.ArchiveBorrowTask;
import com.hiultra.archive.utils.sgsm.SM2Util;
import com.hiultra.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

//@Component
public class NetRepeater {

    @Autowired
    private RestTemplate restTemplate;


     <T> T repeater(String url, Object request ) throws JsonProcessingException, URISyntaxException {
        // 将JsonData对象转换为JSON字符串
        String jsonString = new ObjectMapper().writeValueAsString(request);
        System.out.println(jsonString);

        // 公钥加密
        String arcFacialDataVoEncryptionString = new SM2Util().encryptBySM2(jsonString, "pubKey");
        System.out.println("档案借阅任务查询接口公钥加密后:**********" + arcFacialDataVoEncryptionString);

        String resultEncryption = restTemplate.postForObject(new URI(url), arcFacialDataVoEncryptionString, String.class);

        // 私钥解密获取结果
        String deStr = new SM2Util().decryptBySM2(resultEncryption, "priKey");
        System.out.println("档案借阅任务查询接口私钥解密后" + deStr);
        HashMap hashMap = JSON.parseObject(deStr, HashMap.class);
        System.out.println(hashMap);

        if (hashMap == null || !hashMap.containsKey("code") || !hashMap.get("code").equals("00000")) {
            System.out.println("档案借阅任务查询接口查询借阅信息失败");
            throw new ServiceException(hashMap.get("message").toString());
        }

        System.out.println("档案借阅任务查询接口查询借阅信息成功");

         //tClass.isArray();
         //if(T instanceof List){

       // }
        List<ArchiveBorrowTask>
        arcBorrowVoList = (List<ArchiveBorrowTask>) JSON.parseArray(hashMap.get("data").toString(), ArchiveBorrowTask.class);

        return null;
    }
}
